package com.ciwong.epaper.modules.reciteWords.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.reciteWords.bean.JiaoCaiBean;
import com.ciwong.epaper.modules.reciteWords.bean.SelectVocabularyBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/8/22.
 */
public class PopWindowRecyclerViewAdapter extends BaseRecyclerViewAdapter<SelectVocabularyBean,RecyclerView.ViewHolder> {
	public  static  final  int  ITEM_TYPE_ALL          =0;
	public  static  final  int  ITEM_TYPE_JIAOCAI      =1;
	private static final String TAG                    = "PopWindowRecyclerViewAdapter";

	private boolean mIsSelected = false;
	public PopWindowRecyclerViewAdapter(Context context, List<SelectVocabularyBean> datas) {
		super(context);
		mDatas = new ArrayList<>();
		mDatas.addAll(datas);
	}

	@Override
	public int getItemViewType(int position) {
		if(position==0){
			return ITEM_TYPE_ALL;
		}else{
			return ITEM_TYPE_JIAOCAI;
		}

	}

	@Override
	public int getItemCount() {
		return mDatas==null?0:mDatas.size()+1 ;
	}
	public void setIsSelectedAll(String rightTopText) {
		mIsSelected = "全部".equals(rightTopText);
		notifyDataSetChanged();
	}
	public void updateDatas(List<SelectVocabularyBean> datas,String rightTopText) {
		mIsSelected = "全部".equals(rightTopText);
		updateDatas(datas);
	}
	@Override
	public RecyclerView.ViewHolder onCreateMyViewHolder(@NonNull ViewGroup parent, int viewType) {
		if(viewType== ITEM_TYPE_ALL){
			View inflate = LayoutInflater.from(mContext).inflate(R.layout.layout_pop_window_alltype_item, parent, false);
			return  new AllTypeViewHolder(inflate);
		}else{
			View inflate = LayoutInflater.from(mContext).inflate(R.layout.layout_pop_window_jiaocai_item, parent, false);
			return new JiaoCaiViewHolder(inflate);
		}

	}

	@Override
	public void onBindMyViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
		int itemViewType = getItemViewType(position);
		if(itemViewType== ITEM_TYPE_ALL){
			AllTypeViewHolder 	allTypeViewHolder = (AllTypeViewHolder) holder;
			JiaoCaiBean jiaoCaiBean = new JiaoCaiBean("全部");
			jiaoCaiBean.setSelected(mIsSelected);
			allTypeViewHolder.bindData(jiaoCaiBean);
		}else{
			if(position<1){
				return;
			}
			JiaoCaiViewHolder 	jiaoCaiViewHolder = (JiaoCaiViewHolder) holder;
			jiaoCaiViewHolder.bindData(mDatas.get(position-1));
		}
	}



	public class AllTypeViewHolder extends RecyclerView.ViewHolder {
		TextView                    mTvJiaoCaiName;
		public AllTypeViewHolder(View itemView) {
			super(itemView);
			mTvJiaoCaiName = itemView.findViewById(R.id.tv_jiaocai_name);
			mTvJiaoCaiName.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(mOnSelectClickListener!=null){
						mTvJiaoCaiName.setSelected(true);
						mOnSelectClickListener.onItemSelected(-1,new JiaoCaiBean("全部"),"全部");
					}
				}
			});
		}

		public void bindData(JiaoCaiBean jiaoCaiBean ) {
			if(jiaoCaiBean.isSelected()){
				mTvJiaoCaiName.setSelected(true);
			}else{
				mTvJiaoCaiName.setSelected(false);
			}
		}
	}
	public class JiaoCaiViewHolder extends RecyclerView.ViewHolder {
		List<JiaoCaiBean>     mJiaoCaiBeanList;
		TextView                    mTvLevelName;
		RecyclerView                mRecyclerView;
		 JiaoCaiDetailAdapter mJiaoCaiDetailAdapter;
		public JiaoCaiViewHolder(View itemView) {
			super(itemView);
			mTvLevelName = itemView.findViewById(R.id.tv_level_name);
			mRecyclerView = itemView.findViewById(R.id.recycler_view);
			mRecyclerView.setFocusable(false);
			mJiaoCaiBeanList = new ArrayList<>();
			mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
			mJiaoCaiDetailAdapter = new JiaoCaiDetailAdapter(mContext, mJiaoCaiBeanList);
			mRecyclerView.setAdapter(mJiaoCaiDetailAdapter);

		}

		public void bindData(final SelectVocabularyBean selectVocabularyBean) {
			mTvLevelName.setText(selectVocabularyBean.getPeriodName());
			mJiaoCaiDetailAdapter.updateDatas(selectVocabularyBean.getChildren());
			mJiaoCaiDetailAdapter.setOnItemClickListener(new OnItemClickListener<JiaoCaiBean>() {
				@Override
				public void onItemClick(int positon, JiaoCaiBean jiaoCaiBean) {
					if(mOnSelectClickListener!=null){
						mOnSelectClickListener.onItemSelected(positon,jiaoCaiBean,selectVocabularyBean.getPeriodName());
					}
				}
			});
		}
	}


	public void setOnSelectClickListener(OnSelectClickListener onSelectClickListener) {
		mOnSelectClickListener = onSelectClickListener;
	}

	public OnSelectClickListener mOnSelectClickListener;
	public    interface OnSelectClickListener{
		void  onItemSelected(int positon, JiaoCaiBean jiaoCaiBean,String periodName);
	}
}
